package com.xyauto.theme.view.seekbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;


public abstract class BaseSeekBar extends View {

    private static final String TAG = "BaseSeekBar";

    public BaseSeekBar(Context context) {
        this(context, null);
    }

    public BaseSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public BaseSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr);

    }

    private int mThumbRes, mHighlightRes, mBackgroundRes;
    private Drawable mThumb, mHighLightDrawable, mBackground;
    private int mProgressWidth = 4;
    private int mArcWidth = 2;
    /**
     * The Angle to start drawing this Arc from twelve`clock
     */
    private int mStartAngle = 0;
    /**
     * Current Progress
     */
    private int mProgress = 0;
    private int mMax = 100;
    private int mMaxSweepAngle = 360;
    private int mEffect;
    private int mHeadshinemargin;
    private int mTailshinemargin;
    private int mHlheadshinemargin;
    private int mHltailshinemargin;
    private int mReinitHltailshinemargin;
    private int mReinitHlheadshinemargin;
    private int mReinitTailshinemargin;
    private int mReinitHeadshinemargin;
    private int mReinitMARGINLEFTOFFSET;
    private int mProgresstype;
    private int mDirection;
    private int mNewStyle;
    private int MARGINLEFTOFFSET = 0;
    private boolean mIsNine = false;

    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        //TypedArray a = context.obtainStyledAttributes(attrs, com.xyauto.theme.R.styleable.BaseSeekBar, defStyleAttr, defStyleRes);
        if (attrs == null) return;
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if ("highlight".equals(attrName)) {
                mHighlightRes = attrs.getAttributeResourceValue(i, 0);
                mHighLightDrawable = getSkinDrawable(mHighlightRes);
            }
            if ("thumb".equals(attrName)) {
                mThumbRes = attrs.getAttributeResourceValue(i, 0);

                mThumb = getSkinDrawable(mThumbRes);

            }
            if ("backgroud".equals(attrName)) {
                mBackgroundRes = attrs.getAttributeResourceValue(i, 0);

                mBackground = getSkinDrawable(mBackgroundRes);

            }
            if ("max".equals(attrName)) {
                mMax = attrs.getAttributeIntValue(i, mMax);
            }
            if ("progresss".equals(attrName)) {
                mProgress = attrs.getAttributeIntValue(i, mProgress);
            }
            if ("arcWidth".equals(attrName)) {
                mArcWidth = attrs.getAttributeIntValue(i, mArcWidth);
            }

            if ("startAngle".equals(attrName)) {
                mStartAngle = attrs.getAttributeIntValue(i, mStartAngle);
            }
            if ("endAngle".equals(attrName)) {
                mMaxSweepAngle = attrs.getAttributeIntValue(i, mMaxSweepAngle);
            }

            if ("thumboffset".equals(attrName)) {
                mReinitMARGINLEFTOFFSET = this.MARGINLEFTOFFSET = attrs.getAttributeIntValue(i, MARGINLEFTOFFSET);
            }

            if ("progresstype".equals(attrName)) {
                mProgresstype = attrs.getAttributeIntValue(i, mProgresstype);
            }

            if ("progressdirection".equals(attrName)) {
                mDirection = attrs.getAttributeIntValue(i, mDirection);
            }

            if ("newstyle".equals(attrName)) {
                mNewStyle = attrs.getAttributeIntValue(i, mNewStyle);
            }

            if ("headshinemargin".equals(attrName)) {
                this.mReinitHeadshinemargin = this.mHeadshinemargin = attrs.getAttributeIntValue(i, mHeadshinemargin);
            }

            if ("tailshinemargin".equals(attrName)) {
                this.mReinitTailshinemargin = this.mTailshinemargin = attrs.getAttributeIntValue(i, mTailshinemargin);
            }

            if ("hlheadshinemargin".equals(attrName)) {
                this.mReinitHlheadshinemargin = this.mHlheadshinemargin = attrs.getAttributeIntValue(i, mHlheadshinemargin);
            }

            if ("hltailshinemargin".equals(attrName)) {
                this.mReinitHltailshinemargin = this.mHltailshinemargin = attrs.getAttributeIntValue(i, mHltailshinemargin);
            }

            if ("effect".equals(attrName)) {
                mEffect = attrs.getAttributeIntValue(i, mEffect);
            }

        }

//        mThumb = a.getDrawable(R.styleable.SeekArc_thumb);
//        //Log.i(TAG, "init: mThumb:"+a.getResourceId(R.styleable.SeekArc_highlight,0));
//        mHighLightDrawable = a.getDrawable(R.styleable.SeekArc_highlight);
//        mBackground = a.getDrawable(R.styleable.SeekArc_backgroud);
//        mMax = a.getInteger(R.styleable.SeekArc_max, mMax);
//        mProgress = a.getInteger(R.styleable.SeekArc_progress, mProgress);
//        mProgressWidth = (int) a.getDimension(R.styleable.SeekArc_progressWidth, mProgressWidth);
//        mArcWidth = (int) a.getDimension(R.styleable.SeekArc_arcWidth, mArcWidth);
//
//            if("tailshinemargin".equals(attrName)) {
//                this.mReinitTailshinemargin = this.mTailshinemargin = attrs.getAttributeIntValue(i,mTailshinemargin);
//            }
//
//            if("hlheadshinemargin".equals(attrName)) {
//                this.mReinitHlheadshinemargin = this.mHlheadshinemargin =  attrs.getAttributeIntValue(i,mHlheadshinemargin);
//            }
//
//            if("hltailshinemargin".equals(attrName)) {
//                this.mReinitHltailshinemargin = this.mHltailshinemargin = attrs.getAttributeIntValue(i,mHltailshinemargin);
//            }
//
//            if("effect".equals(attrName)) {
//                mEffect = attrs.getAttributeIntValue(i,mEffect);
//            }
//
//        }
//
////        mThumb = a.getDrawable(R.styleable.SeekArc_thumb);
////        //Log.i(TAG, "init: mThumb:"+a.getResourceId(R.styleable.SeekArc_highlight,0));
////        mHighLightDrawable = a.getDrawable(R.styleable.SeekArc_highlight);
////        mBackground = a.getDrawable(R.styleable.SeekArc_backgroud);
////        mMax = a.getInteger(R.styleable.SeekArc_max, mMax);
////        mProgress = a.getInteger(R.styleable.SeekArc_progress, mProgress);
////        mProgressWidth = (int) a.getDimension(R.styleable.SeekArc_progressWidth, mProgressWidth);
////        mArcWidth = (int) a.getDimension(R.styleable.SeekArc_arcWidth, mArcWidth);
////
////        mStartAngle = a.getInt(R.styleable.SeekArc_startAngle, mStartAngle);
////        mMaxSweepAngle = a.getInt(R.styleable.SeekArc_endAngle, mMaxSweepAngle);// set
////
////        mReinitMARGINLEFTOFFSET = this.MARGINLEFTOFFSET = a.getInteger(R.styleable.SeekArc_thumboffset, MARGINLEFTOFFSET);
////        mProgresstype = a.getInteger(R.styleable.SeekArc_progresstype, mProgresstype);
////        mDirection = a.getInteger(R.styleable.SeekArc_progressdirection, this.mDirection);
////        mNewStyle = a.getInteger(R.styleable.SeekArc_newstyle, this.mNewStyle);
//        if (mHighLightDrawable instanceof NinePatchDrawable) {
//            mIsNine = true;
//        }
//        if (this.mNewStyle == 1 && !mIsNine) {
//            this.mReinitHeadshinemargin = this.mHeadshinemargin =mHeadshinemargin + 7;
//            this.mReinitTailshinemargin = this.mTailshinemargin = mTailshinemargin+ 7;
//        } else {
//          //  this.mReinitHeadshinemargin = this.mHeadshinemargin = a.getInteger(R.styleable.SeekArc_headshinemargin, mHeadshinemargin);
//         //   this.mReinitTailshinemargin = this.mTailshinemargin = a.getInteger(R.styleable.SeekArc_tailshinemargin, mTailshinemargin);
//        }
//       // this.mReinitHlheadshinemargin = this.mHlheadshinemargin = a.getInteger(R.styleable.SeekArc_hlheadshinemargin, mHlheadshinemargin);
//      //  this.mReinitHltailshinemargin = this.mHltailshinemargin = a.getInteger(R.styleable.SeekArc_hltailshinemargin, mHltailshinemargin);
//     //   mEffect = a.getInt(R.styleable.SeekArc_effect, mEffect);
//        a.recycle();
    }


    protected Drawable getSkinDrawable(int res) {
        if (res != 0) {
            return ContextCompat.getDrawable(getContext(), res);
        }
        return null;
    }


    protected abstract void refreshDrawable();


    public Drawable getThumb() {
        return mThumb;
    }

    public void setThumb(Drawable mThumb) {
        this.mThumb = mThumb;
    }

    public Drawable getHighLightDrawable() {
        return mHighLightDrawable;
    }

    public void setHighLightDrawable(Drawable mHighLightDrawable) {
        this.mHighLightDrawable = mHighLightDrawable;
    }

    public Drawable getBackground() {
        return mBackground;
    }

    public void setBackground(Drawable mBackground) {
        this.mBackground = mBackground;
    }

    public int getProgressWidth() {
        return mProgressWidth;
    }

    public void setProgressWidth(int mPprogressWidth) {
        this.mProgressWidth = mPprogressWidth;
    }

    public int getArcWidth() {
        return mArcWidth;
    }

    public void setArcWidth(int mArcWidth) {
        this.mArcWidth = mArcWidth;
    }

    public int getStartAngle() {
        return mStartAngle;
    }

    public void setStartAngle(int mStartAngle) {
        this.mStartAngle = mStartAngle;
    }

    public int getProgress() {
        return mProgress;
    }

    public void setProgress(int mProgress) {
        this.mProgress = mProgress;
    }

    public int getMax() {
        return mMax;
    }

    public void setMax(int mMax) {
        this.mMax = mMax;
    }

    public int getMaxSweepAngle() {
        return mMaxSweepAngle;
    }

    public void setMaxSweepAngle(int mMaxSweepAngle) {
        this.mMaxSweepAngle = mMaxSweepAngle;
    }

    public int getEffect() {
        return mEffect;
    }

    public void setEffect(int mEffect) {
        this.mEffect = mEffect;
    }

    public int getHeadshinemargin() {
        return mHeadshinemargin;
    }

    public void setHeadshinemargin(int mHeadshinemargin) {
        this.mHeadshinemargin = mHeadshinemargin;
    }

    public int getTailshinemargin() {
        return mTailshinemargin;
    }

    public void setTailshinemargin(int mTailshinemargin) {
        this.mTailshinemargin = mTailshinemargin;
    }

    public int getHlheadshinemargin() {
        return mHlheadshinemargin;
    }

    public void setHlheadshinemargin(int mHlheadshinemargin) {
        this.mHlheadshinemargin = mHlheadshinemargin;
    }

    public int getHltailshinemargin() {
        return mHltailshinemargin;
    }

    public void setHltailshinemargin(int mHltailshinemargin) {
        this.mHltailshinemargin = mHltailshinemargin;
    }

    public int getReinitHltailshinemargin() {
        return mReinitHltailshinemargin;
    }

    public void setReinitHltailshinemargin(int mReinitHltailshinemargin) {
        this.mReinitHltailshinemargin = mReinitHltailshinemargin;
    }

    public int getReinitHlheadshinemargin() {
        return mReinitHlheadshinemargin;
    }

    public void setReinitHlheadshinemargin(int mReinitHlheadshinemargin) {
        this.mReinitHlheadshinemargin = mReinitHlheadshinemargin;
    }

    public int getReinitTailshinemargin() {
        return mReinitTailshinemargin;
    }

    public void setReinitTailshinemargin(int mReinitTailshinemargin) {
        this.mReinitTailshinemargin = mReinitTailshinemargin;
    }

    public int getReinitHeadshinemargin() {
        return mReinitHeadshinemargin;
    }

    public void setReinitHeadshinemargin(int mReinitHeadshinemargin) {
        this.mReinitHeadshinemargin = mReinitHeadshinemargin;
    }

    public int getReinitMARGINLEFTOFFSET() {
        return mReinitMARGINLEFTOFFSET;
    }

    public void setReinitMARGINLEFTOFFSET(int mReinitMARGINLEFTOFFSET) {
        this.mReinitMARGINLEFTOFFSET = mReinitMARGINLEFTOFFSET;
    }

    public int getProgresstype() {
        return mProgresstype;
    }

    public void setProgresstype(int mProgresstype) {
        this.mProgresstype = mProgresstype;
    }

    public int getDirection() {
        return mDirection;
    }

    public void setDirection(int mDirection) {
        this.mDirection = mDirection;
    }

    public int getNewStyle() {
        return mNewStyle;
    }

    public void setNewStyle(int mNewStyle) {
        this.mNewStyle = mNewStyle;
    }

    public boolean isIsNine() {
        return mIsNine;
    }

    public void setIsNine(boolean mIsNine) {
        this.mIsNine = mIsNine;
    }
}